
#include "playscene.h"
#include<QMenuBar>
#include<QPainter>
#include<QLabel>
#include"mycoin.h"
#include"dataconfig.h"
#include<QTimer>
#include<QDebug>
#include<QPropertyAnimation>

//PlayScene::PlayScene(QWidget *parent)
//    : QMainWindow{parent}
//{

//}

PlayScene::PlayScene(int levelNum)
{
    this->levelIndex=levelNum;
    //初始化游戏场景
    this->setFixedSize(340,560);
    this->setWindowIcon(QIcon(":/picture/level.jpeg"));
    this->setWindowTitle("哄阿尼亚开心");
    QMenuBar* bar=menuBar();
    setMenuBar(bar);
    QMenu*startMenu=bar->addMenu("开始");
    QAction*quitAction=startMenu->addAction("退出");
    connect(quitAction,&QAction::triggered,[=](){
        this->close();
    });
    QAction*quitAction1=startMenu->addAction("返回");
    connect(quitAction1,&QAction::triggered,[=](){
        emit this->chooseSceneBack();
    });
    QLabel*label=new QLabel;
    label->setParent(this);
    QFont font;
    font.setFamily("华文新魏");
    font.setPointSize(20);
    QString str1=QString("Level:%1").arg(this->levelIndex);
    label->setFixedSize(100,40);
    label->move(20,515);
    label->setFont(font);
    label->setText(str1);
    //初始化每个关卡的二维数组
    DataConfig config;
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            this->gameArray[i][j]=config.mData[this->levelIndex][i][j];
        }
    }
    //胜利图片显示
    QLabel*winLabel=new QLabel;
    QPixmap tmpPix;
    tmpPix.load(":/win1/win(1).jpg");
    winLabel->setGeometry(0,0,tmpPix.width(),tmpPix.height());
    winLabel->setPixmap(tmpPix);
    winLabel->setParent(this);
    winLabel->move((this->width()-tmpPix.width())*0.5,-tmpPix.height());
    //显示金币背景图案
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            QPixmap pix=QPixmap("://square/square.jpg");
            QLabel* label=new QLabel;
            label->setGeometry(0,0,pix.width(),pix.height());
            label->setPixmap(pix);
            label->setParent(this);
            label->move(65+i*50,170+j*50);
            //创建金币
            QString str;
            if(this->gameArray[i][j]==1)
            {
                str=":/,/coin2/coin01.jpg";
            }
            else
            {
                str=":/,/coin2/coin02.jpg";
            }
            MyCoin* coin=new MyCoin(str);
            coin->setParent(this);

            coin->move(73+i*50,177+j*50);
            //给金币属性赋值
            coin->posX=i;
            coin->posY=j;
            coin->flag=gameArray[i][j];
            coinbtn[i][j]=coin;
            connect(coin,&MyCoin::clicked,[=](){
                coin->changeFlag();
                this->gameArray[i][j]=this->gameArray[i][j]==0?1:0;
                //翻转周围
                QTimer::singleShot(300,this,[=](){
                    if(coin->posX+1<=3)//右侧
                    {
                        coinbtn[coin->posX+1][coin->posY]->changeFlag();
                        this->gameArray[coin->posX+1][coin->posY]=this->gameArray[coin->posX+1][coin->posY]==0?1:0;
                    }
                    if(coin->posX-1>=0)//左侧
                    {
                        coinbtn[coin->posX-1][coin->posY]->changeFlag();
                        this->gameArray[coin->posX-1][coin->posY]=this->gameArray[coin->posX-1][coin->posY]==0?1:0;
                    }
                    if(coin->posY+1<=3)//上侧
                    {
                        coinbtn[coin->posX][coin->posY+1]->changeFlag();
                        this->gameArray[coin->posX][coin->posY+1]=this->gameArray[coin->posX][coin->posY+1]==0?1:0;
                    }
                    if(coin->posY-1>=0)//下侧
                    {
                        coinbtn[coin->posX][coin->posY-1]->changeFlag();
                        this->gameArray[coin->posX][coin->posY-1]=this->gameArray[coin->posX][coin->posY-1]==0?1:0;
                    }
                    //判断是否胜利
                    this->isWin=true;
                    for(int i=0;i<4;i++)
                    {
                        for(int j=0;j<4;j++)
                        {
                            if(coinbtn[i][j]->flag==false)
                            {
                                this->isWin=false;
                                break;
                            }
                        }
                    }
                    if(this->isWin==true)
                    {
                        qDebug()<<"游戏胜利";
                        for(int i=0;i<4;i++)
                        {
                            for(int j=0;j<4;j++)
                            {
                                coinbtn[i][j]->isWin=true;
                            }
                        }
                        //将胜利的图片移动下来
                        QPropertyAnimation*animation=new QPropertyAnimation(winLabel,"geometry");
                        //设置时间间隔
                        animation->setDuration(1000);
                        //设置开始位置
                        animation->setStartValue(QRect(winLabel->x(),winLabel->y(),winLabel->width(),winLabel->height()));
                        //设置结束位置
                        animation->setEndValue(QRect(winLabel->x(),winLabel->y()+160,winLabel->width(),winLabel->height()));
                        //设置缓和曲线
                        animation->setEasingCurve(QEasingCurve::OutBounce);
                        //执行动画
                        animation->start();
                    }
                });

            });
        }
    }

}

void PlayScene::paintEvent(QPaintEvent*)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/a/background3/pure_background.jpeg");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);


}